package com.epfresh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.SearchActivity;
import com.epfresh.api.entity.RequestEntityMap;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.widget.tablayout.SlidingTabLayout;
import com.epfresh.api.widget.tablayout.listener.OnTabSelectListener;
import com.epfresh.bean.Category;
import com.epfresh.bean.Market;
import com.epfresh.bean.ProductCategory;
import com.epfresh.bean.StoreDetail;
import com.epfresh.delegate.DcGoodsDelegate;
import com.epfresh.global.BaseActivity;
import com.epfresh.global.BaseFragment;
import com.epfresh.global.DataManager;
import com.epfresh.views.MultiStateView;
import com.epfresh.views.dialog.BaseDialogView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
 * @date create 17-6-8
 * @author wangzhanghuan
 * @Description
 * @version 1.0
 */
public class DcGoodsFragment extends BaseFragment implements OnTabSelectListener {
    private RelativeLayout rl_no_data;
    private MultiStateView cg_mult_state_view;

    DcGoodsDelegate cgDelegate;


    private SlidingTabLayout lyTab;
    private List<Category> listCategory = new ArrayList<>();

    MultiStateView multiStateView;

    StoreDetail storeDetail;

    @Override
    public String getBaseTag() {
        return "categoryGoodsFragment";
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_dc_goods,container, false);


        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        FragmentActivity activity = getActivity();

        rl_no_data=view.findViewById(R.id.rl_no_data);
        multiStateView=view.findViewById(R.id.cg_mult_state_view);

        if (listCategory.size()>0){//有数据
            rl_no_data.setVisibility(View.GONE);
            multiStateView.setVisibility(View.VISIBLE);

            cgDelegate = new DcGoodsDelegate((BaseActivity) activity, view);

            cgDelegate.setStoreDetail(storeDetail);
//            multiStateView = (MultiStateView) view.findViewById(R.id.cg_mult_state_view);
            multiStateView.setViewForState(R.layout.loading_view, MultiStateView.VIEW_STATE_LOADING);
            multiStateView.setViewForState(R.layout.empty_goods, MultiStateView.VIEW_STATE_EMPTY);
            multiStateView.setViewForState(R.layout.error_view, MultiStateView.VIEW_STATE_ERROR);
            multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);

            multiStateView.setOnErrorClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

//                requestCategory(null);
                }
            });
            multiStateView.setOnEmptyClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                updateData();
                }
            });

            lyTab = (SlidingTabLayout) view.findViewById(R.id.ly_navigation);
            lyTab.setOnTabSelectListener(this);
            updateCategoryTitle(getCategoryFirst());
            updateListStatus(0);
        }else {//无数据
            rl_no_data.setVisibility(View.VISIBLE);
            multiStateView.setVisibility(View.GONE);
        }



    }

    public void updateGoodsCollect(){
        if (getActivity() != null && cgDelegate != null){

            cgDelegate.updateGoodsCollect();
        }
    }

    public void requestCount(){
        if (getActivity() != null && cgDelegate != null){
            cgDelegate.requestCount();
        }
    }
    private void updateCategoryTitle(String[] titles) {
        if (titles == null || titles.length == 0){
            return;
        }

        lyTab.setTitles(titles);
    }

    private String[] getCategoryFirst() {
        if (listCategory.size() > 0) {
            String[] strings = new String[listCategory.size()];
            for (int i = 0; i < listCategory.size(); i++) {
                Category category = listCategory.get(i);
                strings[i] = category.getName();
            }
            return strings;
        }
        return new String[0];
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()) {
            case R.id.iv_search:
                Intent search = new Intent(getActivity(), SearchActivity.class);
                startActivity(search);
                break;
            case R.id.tv_market:
            case R.id.iv_market:
//                openMarket();
                break;


        }
    }


    Set<Integer> currentSet;
    List<String> marketIds;

    public void updateData(List<Category> list) {
        if (currentSet != null) {
            currentSet.clear();
        }

        listCategory.clear();
        listCategory.addAll(list);
        updateCategoryTitle(getCategoryFirst());
        updateListStatus(0);

    }

    public void updateList(List<Category> list) {
        if (list != null) {
            listCategory.clear();
            listCategory.addAll(list);
        }
    }


    public void setStoreDetail(StoreDetail storeDetail) {
        this.storeDetail = storeDetail;
    }

    private OnRequestListener<List<Category>> onRequestListener = new OnRequestListener<List<Category>>() {
        @Override
        public List<Category> jsonToObj(String s) {
            return new Gson().fromJson(s, new TypeToken<List<Category>>() {
            }.getType());
        }

        @Override
        public void onFail(int i, Object o, Object o1) {
            multiStateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
        }

        @Override
        public void onResponseError(int i, Object o, Object o1) {
            multiStateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
        }

        @Override
        public void onStart(Object o) {
            multiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        }

        @Override
        public void onResponse(ResponseEntity<List<Category>> responseEntity, Object o) {
            multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);

            List<Category> responseElement = responseEntity.getResponseElement();
            if (responseElement != null) {
                listCategory.clear();
                listCategory.addAll(responseElement);
                updateCategoryTitle(getCategoryFirst());
                updateListStatus(0);
            }


        }
    };


    @Override
    public void onTabSelect(int i) {

        updateListStatus(i);
    }

    private void updateListStatus(int i) {
        if (cgDelegate != null){
            cgDelegate.dismissFilter();
        }
        if (i < listCategory.size()) {
            Category category = listCategory.get(i);
            List<ProductCategory> categories = category.getCategories();
            if (categories != null && categories.size() > 0) {
                for (int j=0;j<categories.size();j++){
                    ProductCategory productCategory = categories.get(j);
                    productCategory.setSelect(false);
                }
                ProductCategory productCategory = categories.get(0);
                productCategory.setSelect(true);
            }
            cgDelegate.updateList(category.getId(), categories, marketIds);
        }
    }

    @Override
    public void onTabReselect(int i) {

    }


    public void updateCount(){
        if(getActivity() != null && cgDelegate != null){
            cgDelegate.requestCount();
        }
    }



}
